var click = {};
click.divs = document.getElementById("warp").getElementsByTagName("div");
click.obstacle = [];//判断是否有障碍
click.inner= [];
var road = {}; //路集合
road.coor = 60; //起始坐标
road.step = [0,0,0,0,0,0]; //步数集合
road.obstacle = [0,0,0,0,0,0]; //计算障碍物
var dir = [-11,-1,11,12,1,-10]; //六个方向
road.prority = [0,0,0,0,0,0]; //优先级别
var num = 0; //记录步
window.onload = function(){
    end();
}
//点击事件
for (var i = 0; i < click.divs.length; i++) {
	click.divs[i].inner	 = i;
	click.divs[i].onclick = function (){
		if (click.obstacle[this.inner] == 0) {
			click.divs[this.inner].style.background = "red";
			click.obstacle[this.inner] = 1;
			move(road.coor);
			num++;
		}	
	}
}
//走路方向 i是方向
function direction(i,coor){
	// 0左上 1左 2左下 3右下 4右 5右上	
	if(coor%22<12) {
		if (i == 0 || i == 5 || i == 3 || i == 2) {
		 	coor-=1;
		 }
	}
	return coor + dir[i]; //返回猫坐标
}
//寻找附近方向
function newdir(i,coor){
	var ileft = 0; //左方向
	var iright = 0;//右方向
	var lstep = 0;
	var rstep = 0;
	if (i == 0) {
		ileft = 5;
		iright = 1;
	}else if(i == 5){
		iright = 0;
		ileft = 4;
	}else{
		ileft = i - 1;
		iright = i + 1;
	}
	if (click.obstacle[direction(ileft,coor)] == 0) {
		lstep = newbook(i,direction(ileft,coor));
	}else{
		lstep = 99;
	}
	if(click.obstacle[direction(iright,coor)] == 0){
		rstep = newbook(i,direction(iright,coor));
	}else{
		rstep = 99;
	}
	// alert(i+"  "+lstep+"  "+rstep);
	if (lstep <= rstep && lstep != 99) {
		return direction(ileft,coor);
	}else if(lstep > rstep){
		return direction(iright,coor);
	}else{
		return 0;
	}
}
//二次判断路程
function newbook(i,coor){
	var step = 0;
	while(1){
		if (coor < 12 || coor > 110 || coor%11 == 0|| coor%11 == 10) {
				break;
		}
		if (click.obstacle[direction(i,coor)] == 0) {
			coor = direction(i,coor);
		}else{
			if (newdir(i,coor) == 0) { 
					step = 99;//路被堵了,跳出此寻路
					break;
				}else{
					coor = newdir(i,coor);
				}
		}
		step =+ 1;
	}
	return step;
}
//路
function book(){
	road.step = [0,0,0,0,0,0]; 
	road.obstacle = [0,0,0,0,0,0];
	for (var i = 5; i >= 0; i--) {
		//获取坐标
		var a;
		var coor = road.coor;
		while(1){
			//判读是否逃出
			if (coor < 12 || coor > 110 || coor%11 == 0|| coor%11 == 10) {
				break;
			}
			//判断方向是否可行
			//记录路
			if (click.obstacle[direction(i,coor)] == 0) {
				coor = direction(i,coor);
			}else{
				a = newdir(i,coor);
				if (a == 0) { 
					road.step[i] = 99;//路被堵了,跳出此寻路
					break;
				}else{
					coor = a;
					road.obstacle[i]+=1;
				}
			}
			road.step[i] += 1;
		}
	}
}
//移动
function move(coor){
	book();
	var min = 0;
	for(var i = 1;i<road.step.length;i++){
		if (road.step[min] > road.step[i]) {
			min = i;
		}else if(road.step[min] == road.step[i]){
			if (road.obstacle[min] > road.obstacle[i]) {
				 min = i;
			}
		}
	}
	click.divs[coor].style.background = "yellow";
	if (click.obstacle[direction(min,coor)] == 0) {
		coor = direction(min,coor);	
	}else{
		coor = newdir(min,coor);
	}
	click.divs[coor].style.background = "blue";
	road.coor = coor;
	if (road.coor < 12 || road.coor > 110 || road.coor%11 == 0 || road.coor%11 == 10) {
		alert("游戏结束");
		end();
	}
}
//重置
function end(){
	road.coor = 60;
	num = 0; 

	for (var i = 0; i < click.divs.length; i++) {
		click.divs[i].style.background = "yellow";
		click.obstacle[i] = 0;
	}
	click.divs[road.coor].style.background = "blue";
	//随机8个障碍
	for (var i = 0; i < 8; i++) {
		var round = Math.round(Math.random()*120);
		if (round == 60) {
			i--;
		}else{
		click.divs[round].style.background = "red";
		click.obstacle[round] = 1;
		}
	}	
}